import 'package:window_manager/window_manager.dart';
import 'dart:ui';
import 'dart:async';

/// 窗口管理器，用于控制窗口行为
class WindowManager {
  static bool _isWindowVisible = false; // 跟踪窗口是否可见

  /// 设置窗口样式
  /// [isFixedSize] 为true时窗口固定大小，false时可调整大小
  static Future<void> setWindowStyle(bool isFixedSize) async {
    try {
      await windowManager.setResizable(!isFixedSize);
    } catch (e) {
      print("设置窗口样式失败: $e");
    }
  }

  /// 设置窗口大小
  static Future<void> setWindowSize(double width, double height) async {
    try {
      await windowManager.setSize(Size(width, height));
    } catch (e) {
      print("设置窗口大小失败: $e");
    }
  }

  /// 设置窗口最小大小
  static Future<void> setWindowMinimumSize(double width, double height) async {
    try {
      await windowManager.setMinimumSize(Size(width, height));
    } catch (e) {
      print("设置窗口最小大小失败: $e");
    }
  }

  /// 隐藏窗口
  static Future<void> hideWindow() async {
    try {
      if (_isWindowVisible) {
        await windowManager.hide();
        _isWindowVisible = false;
      }
    } catch (e) {
      print("隐藏窗口失败: $e");
    }
  }

  /// 显示窗口
  static Future<void> showWindow() async {
    try {
      if (!_isWindowVisible) {
        await windowManager.show();
        _isWindowVisible = true;
      }
    } catch (e) {
      print("显示窗口失败: $e");
    }
  }

  /// 在Flutter渲染完成后显示窗口
  static void showWindowAfterRendering() {
    // 延迟一小段时间确保渲染完成
    Future.delayed(const Duration(milliseconds: 100), () {
      showWindow();
    });
  }
}
